﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class DirectionForm : Form
    {
        private bool m_IsNew = true;
        public bool IsNew
        {
            get { return m_IsNew; }
            set { m_IsNew = value; }
        }

        private string m_Code = "";
        public string Code
        {
            get { return m_Code; }
            set { m_Code = value; }
        }

        private string m_Direction = "";
        public string Direction
        {
            get { return m_Direction; }
            set { m_Direction = value; }
        }

        private bool m_IsDefault = false;
        public bool IsDefault
        {
            get { return m_IsDefault; }
            set { m_IsDefault = value; }
        }

        public DirectionForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                MessageBox.Show("请输入编号！");
                return;
            }
            if (string.IsNullOrEmpty(txtDirection.Text.Trim()))
            {
                MessageBox.Show("请输入流向！");
                return;
            }
            m_Code = txtCode.Text.Trim();
            m_Direction = txtDirection.Text.Trim();
            m_IsDefault = chkIsDefault.Checked;

            string sqlStr = "";
            DataTable dt = new DataTable();
            try
            {
                if (m_IsNew)
                {
                    //判断是否存在相同的编号
                    sqlStr = "select * from Direction where Code = '" + m_Code + "'";
                    dt = CommonHelper.DoSelectOpreation(sqlStr);
                    if (dt.Rows.Count > 0)
                    {
                        MessageBox.Show("已存在相同的编号");
                        return;
                    }
                }
                //判断是否存在相同的物料
                if (m_IsNew)
                    sqlStr = "select * from Direction where Name = '" + m_Direction + "'";
                else
                    sqlStr = "select * from Direction where Name = '" + m_Direction + "' and Code <> '" + m_Code + "'";
                dt = CommonHelper.DoSelectOpreation(sqlStr);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("已存在相同的流向名称");
                    return;
                }
                //是否存在默认项
                if (m_IsDefault)
                {
                    if (m_IsNew)
                        sqlStr = "select * from Direction where IsDefault = true";
                    else
                        sqlStr = "select * from Direction where IsDefault = true and Code <> '" + m_Code + "'";
                    dt = CommonHelper.DoSelectOpreation(sqlStr);
                    if (dt.Rows.Count > 0)
                    {
                        MessageBox.Show(this, "不能同时存在多个默认流向！");
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "保存时异常：" + ex.Message);
                return;
            }       
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void DirectionForm_Load(object sender, EventArgs e)
        {
            if (m_IsNew)
            {
                txtCode.Text = "";
                txtCode.Enabled = true;
                txtDirection.Text = "";
                this.Text = "添加流向";
            }
            else
            {
                txtCode.Text = m_Code;
                txtCode.Enabled = false;
                txtDirection.Text = m_Direction;
                this.chkIsDefault.Checked = m_IsDefault;
                this.Text = "修改流向";
            }
        }
    }
}
